package com.pcbly.web.business.menu;


import java.text.MessageFormat;
//import java.util.Iterator;
import java.util.List;
//import java.util.Set;

import org.springframework.orm.ObjectRetrievalFailureException;

import com.pcbly.web.bean.system.MenuBean;
import com.pcbly.web.business.BaseServiceImp;
import com.pcbly.web.persistence.menu.MenuDao;

public class TreeFactoryServiceImp extends BaseServiceImp<MenuBean> implements TreeFactoryService {
	public TreeFactoryServiceImp() {
		super();
		// TODO Auto-generated constructor stub
	}

	private static MessageFormat tree = new MessageFormat(
	"<div dojoType=\"TreeNode\" title=\"{0}\" value=\"{1}\" checked=\"{2}\" type=\"{3}\">");
	protected MenuDao menuDao;
	

	@Override
	@SuppressWarnings("unused")
	public Object creator(String type, String roleId, String menuId)throws Exception {
		// TODO Auto-generated method stub
		List<MenuBean> list = menuDao.loadTopMenus();
		
		StringBuilder sb=new StringBuilder();
		for(MenuBean ben:list){
			
		}
		return null;
	}
	protected String buildTree(MenuBean menu, String type, String roleId, String menuId) {

		StringBuffer sb = new StringBuffer();

		String[] args = getArgs(menu, type, roleId, menuId);

		sb.append(tree.format(args));

//		Set set = menu.getMenuBeans();
//		Iterator i = set.iterator();
//		while (i.hasNext()) {
//			MenuBean temp = (MenuBean) i.next();
//			sb.append(buildTree(temp, type, roleId, menuId));
//		}

		sb.append("</div>");

		return sb.toString();
	}
	private String[] getArgs(MenuBean menu, String type, String roleId, String menuId) {

		String[] args = new String[4];

		args[0] = menu.getName();
		args[1] = String.valueOf(menu.getId());
		args[2] = "false";
		if ("2".equals(type)) {
			if (roleId != null) {
				try {
					// menuRoleManager.get(new MenuRole(roleId,
					// Integer.parseInt(String.valueOf(menu.getId()))));
					args[2] = "true";
				} catch (ObjectRetrievalFailureException e) {
					// do nothing
				}
			}
		} else {
			if (menuId != null
					&& menuId.equals(menu.getId())) {
				args[2] = "true";
			}
		}
		args[3] = type;
		return args;
	}

	public void setMenuDao(MenuDao menuDao) {
		this.menuDao = menuDao;
	}

}
